/*
 * ZonalFunctions.java
 *
 * Created on February 28, 2005, 4:13 PM
 */

package edu.temple.GUS.MMA.Functions;

import java.util.*;
import java.io.*;
import edu.temple.GUS.MMA.io.*;
import edu.temple.GUS.MMA.DataTypes.*;
import edu.temple.GUS.MMA.Neighborhoods.*;
import edu.temple.GUS.MMA.Exceptions.MMAFunctionInitException;
import edu.temple.GUS.MMA.Functions.ZonalLoop;
import edu.temple.GUS.MMA.util.StatisticalOperator;

/**
 *
 * @author  rkhanna
 */
public class ZonalFunctions {
    
    /** Creates a new instance of ZonalFunctions */
    public ZonalFunctions() 
    {
    }
    
   public Hashtable calculateZonalAdd(Hashtable ht)
    {
     Hashtable ht_single = new Hashtable();        
            Enumeration e = ht.keys();
            while (e.hasMoreElements())
            {
                Float htKey = (Float)e.nextElement();                
                Vector v = (Vector) ht.get(htKey);
                Enumeration e1 = v.elements();                
                float htValue=0;
                    while (e1.hasMoreElements())
                    {
                    Float f1 = (Float) e1.nextElement();                       
                    htValue =  htValue + f1.floatValue();
                    }
                ht_single.put(htKey, new Float(htValue));
            }       
     return ht_single;       
    }

   public Hashtable calculateZonalMean(Hashtable ht)
    {
     Hashtable ht_single = new Hashtable();        
     Enumeration e = ht.keys();
            while (e.hasMoreElements())
            {
                Float htKey = (Float)e.nextElement();                
                Vector v = (Vector) ht.get(htKey);
                int arrsize = v.size();
                float temparr[] = new float[arrsize];
                Enumeration e1 = v.elements();                
                float htValue=0;
                int count=0;
                    while (e1.hasMoreElements())
                    {
                    Float f1 = (Float) e1.nextElement();                       
                    temparr[count] = f1.floatValue();
                    count++;
                    }
                htValue = StatisticalOperator.mean(temparr, arrsize);
                ht_single.put(htKey, new Float(htValue));
            }       
     return ht_single;       
    }
   

   public Hashtable calculateZonalMaximum(Hashtable ht)
    {
     Hashtable ht_single = new Hashtable();        
     Enumeration e = ht.keys();
            while (e.hasMoreElements())
            {
                Float htKey = (Float)e.nextElement();                
                Vector v = (Vector) ht.get(htKey);
                int arrsize = v.size();
                float temparr[] = new float[arrsize];
                Enumeration e1 = v.elements();                
                float htValue=0;
                int count=0;
                    while (e1.hasMoreElements())
                    {
                    Float f1 = (Float) e1.nextElement();                       
                    temparr[count] = f1.floatValue();
                    count++;
                    }
                htValue = StatisticalOperator.max(temparr, arrsize);
                ht_single.put(htKey, new Float(htValue));
            }       
     return ht_single;       
    }

   public Hashtable calculateZonalMinimum(Hashtable ht)
    {
     Hashtable ht_single = new Hashtable();        
     Enumeration e = ht.keys();
            while (e.hasMoreElements())
            {
                Float htKey = (Float)e.nextElement();                
                Vector v = (Vector) ht.get(htKey);
                int arrsize = v.size();
                float temparr[] = new float[arrsize];
                Enumeration e1 = v.elements();                
                float htValue=0;
                int count=0;
                    while (e1.hasMoreElements())
                    {
                    Float f1 = (Float) e1.nextElement();                       
                    temparr[count] = f1.floatValue();
                    count++;
                    }
                htValue = StatisticalOperator.min(temparr);
                ht_single.put(htKey, new Float(htValue));
            }       
     return ht_single;       
    }

   public Hashtable calculateZonalMedian(Hashtable ht)
    {
     Hashtable ht_single = new Hashtable();        
     Enumeration e = ht.keys();
            while (e.hasMoreElements())
            {
                Float htKey = (Float)e.nextElement();                
                Vector v = (Vector) ht.get(htKey);
                int arrsize = v.size();
                float temparr[] = new float[arrsize];
                Enumeration e1 = v.elements();                
                float htValue=0;
                int count=0;
                    while (e1.hasMoreElements())
                    {
                    Float f1 = (Float) e1.nextElement();                       
                    temparr[count] = f1.floatValue();
                    count++;
                    }
                htValue = StatisticalOperator.median(temparr, arrsize);
                ht_single.put(htKey, new Float(htValue));
            }       
     return ht_single;       
    }

   public Hashtable calculateZonalRange(Hashtable ht)
    {
     Hashtable ht_single = new Hashtable();        
     Enumeration e = ht.keys();
            while (e.hasMoreElements())
            {
                Float htKey = (Float)e.nextElement();                
                Vector v = (Vector) ht.get(htKey);
                int arrsize = v.size();
                float temparr[] = new float[arrsize];
                Enumeration e1 = v.elements();                
                float htValue=0;
                int count=0;
                    while (e1.hasMoreElements())
                    {
                    Float f1 = (Float) e1.nextElement();                       
                    temparr[count] = f1.floatValue();
                    count++;
                    }
                htValue = StatisticalOperator.max(temparr, arrsize) - StatisticalOperator.min(temparr);
                ht_single.put(htKey, new Float(htValue));
            }       
     return ht_single;       
    }

   public Hashtable calculateZonalVariance(Hashtable ht)
    {
     Hashtable ht_single = new Hashtable();        
     Enumeration e = ht.keys();
            while (e.hasMoreElements())
            {
                Float htKey = (Float)e.nextElement();                
                Vector v = (Vector) ht.get(htKey);
                int arrsize = v.size();
                float temparr[] = new float[arrsize];
                Enumeration e1 = v.elements();                
                float htValue=0;
                int count=0;
                    while (e1.hasMoreElements())
                    {
                    Float f1 = (Float) e1.nextElement();                       
                    temparr[count] = f1.floatValue();
                    count++;
                    }
                htValue = StatisticalOperator.variance(temparr, arrsize);
                ht_single.put(htKey, new Float(htValue));
            }       
     return ht_single;       
    }  
}
